#include<iostream>
using namespace std;

struct ListNode {
     int val;
     ListNode *next;
     ListNode() : val(0), next(nullptr) {}
     ListNode(int x) : val(x), next(nullptr) {}
     ListNode(int x, ListNode *next) : val(x), next(next) {}
};
 
class Solution {
public:
    ListNode* rotateRight(ListNode* head, int k) {
        ListNode *tail=head;
        int n=0;
        for(ListNode*p=head;p;p=p->next)
        {
            n++;
            tail=p;
        }
        k=k%n;
        ListNode*p=head;
        for(int i=0;i<n-k-1;i++)
        {
            p=p->next;   
        }
        tail->next=head;
        p->next=NULL;
        return head;
   }
};
